<?php
namespace app\modules\admin\controllers;
use Yii;
use yii\web\Controller;
use app\models\Addnews;
use yii\web\Session;
use yii\web\Cookie;
use app\models\Login;
use yii\web\UploadedFile;

class DefaultController extends Controller{
    public $session_id=0;
    public $layout='admin';
    public function init(){
        /*$cookies = Yii::$app->request->cookies;
        $cookies_data=$cookies->getValue('admin');//查询cookie*/
        $session=Yii::$app->session;
        if($session->get('islogin')==1){
            $this->session_id=$session->get('admin_id');
        }else{
            $this->redirect(array('/admin/login/index'));
        }
    }
    public function actionIndex(){
        $model = new Addnews();
        //var_dump($_POST['Addnews']);
        if(isset($_POST['Addnews'])){
            $model->attributes = $_POST['Addnews'];
             //$image = UploadedFile::getInstance($model,'photo');
             $randName = $_FILES['Addnews']['name'];
            //var_dump($image);
            //exit();
           // $ext = $image->getExtension();
            //$randName = uniqid().".".$ext;
            //$dir = "./web/uploads";
            //if(!file_exists($dir)){
                //mkdir($dir,0777,"true");
            //}
            //$image->saveAs($dir."/".$randName);
            $model->photo = $randName;
            $model->pubtime=time();
            if($model->save(false)){
                Yii::$app->session->setFlash('info','添加成功');
            }else{
                Yii::$app->session->setFlash('info','添加失败');
            }
        }
        return $this->render('index',['model'=>$model]);
    }
    public function actionLoginout(){
        $session=Yii::$app->session;
        //$session->remove($this->session_id);
       // $this->redirect(array('/admin/login/index'));
        $session->destroy();//销毁所有session
        $this->redirect(array('/admin/login/index'));
    }



    /**public function actionImage(){
        var_dump($_POST);die;
        if(isset($_POST['photo'])){
            $image = UploadedFile::getInstance($model,'photo');
            //var_dump($image);
            //exit();
            $ext = $image->getExtension();
            $randName = uniqid().".".$ext;
            $dir = "./web/uploads";
            if(!file_exists($dir)){
                mkdir($dir,0777,"true");
            }
            $image->saveAs($dir."/".$randName);
            
            echo $dir."/".$randName;
        }else {
            echo 'null';
        }
    }**/

}
